package com.toman.ui.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.R;
import com.toman.Util.JsonUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.toolbar.ToolBarWhiteActivity;
import com.toman.widget.ClearEditText;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class PwdEditActivity extends ToolBarWhiteActivity {

    @BindView(R.id.et_pwd_old)
    ClearEditText etPwdOld;
    @BindView(R.id.et_pwd_new)
    ClearEditText etPwdNew;
    @BindView(R.id.tv_hint)
    TextView tvHint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd_edit);
        ButterKnife.bind(this);
        initToolBar("修改密码", getResources().getColor(R.color.white));

        initView();
    }

    private void initView() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_save, menu);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
update();
                return false;
            }
        });

        return true;
    }

    public void update() {
        if (etPwdNew.getText().toString().length() < 6){
            ToastUtil.makeText(getApplicationContext(), "新密码长度至少6位！");
            return;
        }


        Map<String, String> map = new HashMap<String, String>();


        map.put("password", etPwdOld.getText().toString());
        map.put("newPassword", etPwdNew.getText().toString());


        String str = JsonUtils.obj2JsonString(map);
        ToastUtil.print("发送json" + str);
        OkHttpUtils
                .post(NewBaseUrl.user_update_pwd)
                .headers("token", token)
                .postJson(str)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "修改密码: " + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());

                        if (code == 0) {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            finish();

                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });
    }
}
